package com.hui.springBoot_demo.girl.controller;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.hui.springBoot_demo.girl.model.Girl;
import com.hui.springBoot_demo.girl.service.GirlService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;
import java.util.Optional;
import java.util.StringTokenizer;

/**
 * TODO: 女生的controller
 *
 * @author chenth
 * @create 2018-07-12 19:51
 **/
@RestController
public class GirlController {

    @Resource
    private GirlService girlService;

    /**
     * 通过GetMapping获取所有女生的信息
     * @return
     */
    @GetMapping(value = "/getGirl")
    public List<Girl> girlList(){
        return girlService.findAll();
    }

    /**
     * 通过postMapping添加一个女生
     * @param cupSize
     * @param age
     * @return
     */
    @PostMapping("/addGirl")
    public Girl addGirl(@RequestParam(value = "cupSize") String cupSize,
                        @RequestParam(value = "age",defaultValue = "20") Integer age){
        Girl girl = new Girl();
        girl.setAge(age);
        girl.setCupSize(cupSize);
        return girlService.save(girl);
    }

    /**
     * 通过id来获取一个女生
     * @param id
     * @return
     */
    @PostMapping("/getGirlById")
    public Girl getGirlById(@RequestParam(value = "id")Integer id){
        System.out.println("id:" + id);
        Girl girl = girlService.getOne(id);
        return girl;
    }

    /**
     * 通过id更新一个女生
     * @param id
     * @param age
     * @param cupSize
     * @return
     */
    @PostMapping("/updateGirl")
    public Girl updateGirl(@RequestParam(value = "id") Integer id , @RequestParam(value="age") Integer age ,
                           @RequestParam(value = "cupSize") String cupSize){
        Girl girl = girlService.getOne(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);
        girlService.save(girl);
        return girl;
    }

    /**
     * 通过id删除一个女生
     * @param id
     * @return
     */
    @PostMapping("/deleteGirl")
    public List<Girl> deleteGirl(@RequestParam(value = "id") Integer id){
        girlService.deleteById(id);
        return girlService.findAll();
    }

    /**
     * 扩展JPA的方法，根据年龄来查找
     * @param age
     * @return
     */
    @PostMapping("/getByAge")
    public List<Girl> getByAge(@RequestParam(value="age") Integer age){
        return girlService.findByAge(age);
    }

    /**
     * 扩展JPA的方法，根据cupSize来查找
     * @param cupSize
     * @return
     */
    @PostMapping("/getByCupSize")
    public List<Girl> getByCupSize(@RequestParam(value="cupSize") String cupSize){
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0 ; i < 10000 ; i ++){
            stringBuffer.append(i);
            stringBuffer.append(",");
        }
        String orgStr = stringBuffer.toString();
        Long start = System.currentTimeMillis();
        for (int i = 0 ; i < 10000 ; i ++){
            orgStr.split(",");
        }
        Long end = System.currentTimeMillis();
        System.out.println(orgStr);
        System.out.println(end-start);

        start = System.currentTimeMillis();
        String orgStr1 = stringBuffer.toString();
        StringTokenizer stringTokenizer = new StringTokenizer(orgStr1,",");
        for (int i = 0 ; i < 10000 ; i ++){
            stringTokenizer.nextToken();
        }
        System.out.println(stringTokenizer);
        end = System.currentTimeMillis();
        System.out.println(end-start);
//        addTwo();
        return girlService.findByCupSize(cupSize);
    }

    /**
     *测试事务同时回滚
     */
    @Transactional
    public void addTwo(){
        Girl girl = new Girl();
        girl.setAge(12);
        girl.setCupSize("D");
        girlService.save(girl);

        Girl girl2 = new Girl();
        girl2.setAge(22/0);
        girl2.setCupSize("C");
        girlService.save(girl2);
    }
}
